package com.oocl.client.process.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.oocl.client.process.ProtocolReceiver;
import com.oocl.client.ui.IChatOperator;
import com.oocl.client.ui.ILogin;
import com.oocl.client.ui.IRegister;
import com.oocl.client.ui.impl.ChatFrame;
import com.oocl.qq.protocol.Protocol;

public class ProtocolReceiverImpl implements ProtocolReceiver {

	private IChatOperator chatOperator;
	private DateFormat format = new SimpleDateFormat("HH:mm");
	private ILogin login;
	private IRegister register;

	public ProtocolReceiverImpl() {
	}

	
	
	public IChatOperator getChatOperator() {
		return chatOperator;
	}

	public void setChatOperator(IChatOperator chatOperator) {
		this.chatOperator = chatOperator;
	}

	public ILogin getLogin() {
		return login;
	}

	public void setLogin(ILogin login) {
		this.login = login;
	}

	public IRegister getRegister() {
		return register;
	}

	public void setRegister(IRegister register) {
		this.register = register;
	}

	@Override
	public void receiveProtocol(Protocol p) {
		// //处理一下协议
		switch (p.getAction()) {
		case Register:
			// //注册失败
			if (this.register != null)
				this.register.registerFail(p.getMsg());
			break;
		case Login:
			// /登录成功
			if (this.login != null)
				this.login.finishLogin(p.getFrom(),getOnlineUserList(p.getMsg()));
			break;
		case Logout:
			/// 登录失败
			if(this.login!=null)
				this.login.loginFail(p.getMsg());
			break;
			
		case Chat:
			String msg = p.getMsg();
			if (this.chatOperator != null)
				this.chatOperator.receiveMsg(p.getFrom().trim(), msg.trim(),
						this.format.format(p.getTime()));
			break;

		case Shake:
			if (this.chatOperator != null)
				this.chatOperator.shake(p.getFrom().trim(),
						format.format(p.getTime()));
			break;
		case List:
			if (this.chatOperator != null) {
				this.chatOperator.updateOnlineUsers(getOnlineUserList(p.getFrom()));
				if (p.getMsg() != null) {
					this.chatOperator.receiveMsg(null, p.getMsg(),
							this.format.format(System.currentTimeMillis()));
				}
			}
//			String onlineUsers = p.getFrom();
//			if (onlineUsers == null || onlineUsers.length() == 0) {
//				if (this.chatOperator != null) {
//					this.chatOperator.updateOnlineUsers(null);
//					if (p.getMsg() != null) {
//						this.chatOperator.receiveMsg(null, p.getMsg(),
//								this.format.format(System.currentTimeMillis()));
//					}
//				}
//				return;
//			}
//			onlineUsers = onlineUsers.trim();
//			System.out.println("onLineUsers: " + onlineUsers);
//			String res1 = onlineUsers.replaceAll("[\\[\\]]", "");
//			System.out.println(res1);
//			if (res1 == null || res1.length() == 0) {
//				if (this.chatOperator != null) {
//					this.chatOperator.updateOnlineUsers(null);
//					if (p.getMsg() != null) {
//						this.chatOperator.receiveMsg(null, p.getMsg(),
//								this.format.format(System.currentTimeMillis()));
//					}
//				}
//			} else {
//				System.out.println("读取到更新下拉的用户列表");
//				List<String> userList = new ArrayList<String>();
//				String[] userItems = res1.split(",");
//
//				if (userItems != null && userItems.length > 0) {
//					for (String userItem : userItems) {
//						userList.add(userItem);
//					}
//				}
//				if (this.chatOperator != null) {
//					this.chatOperator.updateOnlineUsers(userList);
//					if (p.getMsg() != null) {
//						this.chatOperator.receiveMsg(null, p.getMsg(),
//								this.format.format(System.currentTimeMillis()));
//					}
//				}
//			}
			break;
		}
	}

	private List<String> getOnlineUserList(String userListStr) {
		String onlineUsers = userListStr;
		if (onlineUsers == null || onlineUsers.length() == 0) {
			return null;
		}
		onlineUsers = onlineUsers.trim();
		System.out.println("onLineUsers: " + onlineUsers);
		String res1 = onlineUsers.replaceAll("[\\[\\]]", "");
		System.out.println(res1);
		if (res1 == null || res1.length() == 0) {
			return null;
		} else {
			System.out.println("读取到更新下拉的用户列表");
			List<String> userList = new ArrayList<String>();
			String[] userItems = res1.split(",");

			if (userItems != null && userItems.length > 0) {
				for (String userItem : userItems) {
					userList.add(userItem);
				}
			}
			return userList;
		}
	}
}
